hide magic time value from test mode decisions. (#443)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Thu, 19 Dec 2019 16:44:18 +0000 (09:44 -0700)
committerGitHub <noreply@github.com>
Thu, 19 Dec 2019 16:44:18 +0000 (09:44 -0700)
defs.h
garmin_gpi.cc
globals.cc
html.cc
kml.cc
main.cc
osm.cc
random.cc
util.cc
xcsv.cc

diff --git a/defs.h b/defs.h
index 711cb3df63607f59893e64800596b9c2f5e1ba72..fcec22b3fafe428071d9892b24be4ed2f3e78d11 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -1123,6 +1123,7 @@ char* strlower(char* src);
 signed int get_tz_offset();
 time_t mklocaltime(struct tm* t);
 time_t mkgmtime(struct tm* t);
+bool gpsbabel_testmode();
 gpsbabel::DateTime current_time();
 void dotnet_time_to_time_t(double dotnet, time_t* t, int* millisecs);
 signed int month_lookup(const char* m);
index 6cc6b70a774a5625b3b698dd6b44dc74f5831834..fb2c5ada541a5c200fb68e052698df88d4b8322d 100644 (file)
@@ -1462,7 +1462,7 @@ garmin_gpi_rd_init(const QString& fname)
 static void
 garmin_gpi_wr_init(const QString& fname)
 {
-  if (gpi_timestamp != 0) {                    /* not the first gpi output session */
+  if ((gpi_timestamp != 0) && !gpsbabel_testmode()) {                  /* not the first gpi output session */
     time_t t = time(nullptr);
     if (t <= gpi_timestamp) {
       gpi_timestamp++;  /* don't create files with same timestamp */
@@ -1470,7 +1470,7 @@ garmin_gpi_wr_init(const QString& fname)
       gpi_timestamp = t;
     }
   } else {
-    gpi_timestamp = gpsbabel_time;  /* always ZERO during 'testo' */
+    gpi_timestamp = gpsbabel_time;
   }
 
   fout = gbfopen_le(fname, "wb", MYNAME);
@@ -1553,7 +1553,7 @@ garmin_gpi_wr_deinit()
   mkshort_del_handle(&short_h);
   gbfclose(fout);
 
-  if ((opt_sleep) && (gpi_timestamp != 0)) {   /* don't sleep during 'testo' */
+  if ((opt_sleep) && !gpsbabel_testmode()) {   /* don't sleep during 'testo' */
     int sleep = atoi(opt_sleep);
     if (sleep < 1) {
       sleep = 1;
index c1e455c626e70b96ff8f33c636664d01618186ce..0f7ea89062f16798b18fb537ab6460170f8897d4 100644 (file)
@@ -25,7 +25,6 @@
 
 global_options global_opts;
 const char gpsbabel_version[] = VERSION;
-time_t gpsbabel_now;   /* gpsbabel startup-time; initialized in main.c with time() */
-time_t gpsbabel_time;  /* gpsbabel startup-time; initialized in main.c with current_time(), ! ZERO within testo ! */
+time_t gpsbabel_time;  /* gpsbabel startup-time; initialized in main.c with current_time(), ! fixed within testo ! */
 
 posn_status tracking_status;
diff --git a/html.cc b/html.cc
index a9e3f052cc28518c8a640c147016f0f135f7d1a2..0f7e19b7c5a060687595aca1f3abb345f327b95e 100644 (file)
--- a/html.cc
+++ b/html.cc
@@ -253,7 +253,7 @@ data_write()
 
   // Don't write this line when running test suite.  Actually, we should
   // probably not write this line at all...
-  if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) {
+  if (!gpsbabel_testmode()) {
     gbfprintf(file_out, " <meta name=\"Generator\" content=\"GPSBabel %s\">\n", gpsbabel_version);
   }
   gbfprintf(file_out, " <title>GPSBabel HTML Output</title>\n");
diff --git a/kml.cc b/kml.cc
index c45a31855a4b50d68adfcefa771623adfcc8f986..08f147e67689aa1da014cfb52ee8ae15605ce47b 100644 (file)
--- a/kml.cc
+++ b/kml.cc
@@ -2001,7 +2001,7 @@ static void kml_write()
     writer->writeTextElement(QStringLiteral("name"), QStringLiteral("GPS device"));
   }
 
-  if (current_time().isValid()) {
+  if (!gpsbabel_testmode()) {
     writer->writeTextElement(QStringLiteral("snippet"), QStringLiteral("Created ") +
                              current_time().toString());
   }
diff --git a/main.cc b/main.cc
index 5786b384ca0567c8507e8b2d3037736b7f461178..586a80a0c3dcf9b574ff23be99e204b09ff8936d 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -721,10 +721,9 @@ main(int argc, char* argv[])
   global_opts.charset_name.clear();
   global_opts.inifile = nullptr;
 
-  gpsbabel_now = time(nullptr);                        /* gpsbabel startup-time */
-  gpsbabel_time = current_time().toTime_t();                   /* same like gpsbabel_now, but frozen at zero during testo */
+  gpsbabel_time = current_time().toTime_t();                   /* frozen in testmode */
 
-  if (gpsbabel_time != 0) {    /* within testo ? */
+  if (!gpsbabel_testmode()) {  /* within testo ? */
     global_opts.inifile = inifile_init(QString(), MYNAME);
   }
 
diff --git a/osm.cc b/osm.cc
index dd0f80ca923e3b107c0cdf7d478863f32e9d671a..4f4ae906080609c044b4edcdef453f31bf254f58 100644 (file)
--- a/osm.cc
+++ b/osm.cc
@@ -837,7 +837,7 @@ osm_waypt_disp(const Waypoint* wpt)
 
   if (strlen(created_by) !=0) {
     gbfprintf(fout, "    <tag k='created_by' v='%s",created_by);
-    if (gpsbabel_time != 0)
+    if (!gpsbabel_testmode())
       if (strcmp("GPSBabel",created_by)==0) {
         gbfprintf(fout, "-%s", gpsbabel_version);
       }
@@ -892,7 +892,7 @@ osm_rte_disp_trail(const route_head* rte)
 
   if (strlen(created_by) !=0) {
     gbfprintf(fout, "    <tag k='created_by' v='%s",created_by);
-    if (gpsbabel_time != 0)
+    if (!gpsbabel_testmode())
       if (strcmp("GPSBabel",created_by)==0) {
         gbfprintf(fout, "-%s", gpsbabel_version);
       }
@@ -926,7 +926,7 @@ osm_write()
 {
   gbfprintf(fout, "<?xml version='1.0' encoding='UTF-8'?>\n");
   gbfprintf(fout, "<osm version='0.6' generator='GPSBabel");
-  if (gpsbabel_time != 0) {
+  if (!gpsbabel_testmode()) {
     gbfprintf(fout, "-%s", gpsbabel_version);
   }
   gbfprintf(fout, "'>\n");
index 057df2f5c53a6b2be7e8e8fa9b8ef28e34cc3a0f..adc6c7dbd6ce8d0817fea9397faa526060903b6b 100644 (file)
--- a/random.cc
+++ b/random.cc
@@ -24,6 +24,7 @@
 #include <QtCore/QDateTime>  // for QDateTime
 #include <QtCore/QString>    // for QString
 #include <QtCore/QThread>    // for QThread
+#include <QtCore/QVector>    // for QVector
 
 #include "defs.h"
 #include "garmin_fs.h"       // for garmin_fs_t, GMSD_SET, garmin_fs_flags_t, garmin_fs_alloc
@@ -124,16 +125,22 @@ rand_qstr(const int maxlen, const char* fmt)
 }
 
 static void
-random_rd_init(const QString&)
+random_set_generator()
 {
   generator = new std::mt19937;
   if (opt_seed) {
     generator->seed(atoi(opt_seed));
   } else {
-    generator->seed(gpsbabel_now);
+    generator->seed(gpsbabel_time);
   }
 }
 
+static void
+random_rd_init(const QString&)
+{
+  random_set_generator();
+}
+
 static void
 random_rd_deinit()
 {
@@ -243,7 +250,7 @@ random_read()
 
   route_head* head;
   Waypoint* prev = nullptr;
-  QDateTime time = QDateTime::fromTime_t(gpsbabel_time);
+  QDateTime time = current_time();
 
   int points = (opt_points) ? atoi(opt_points) : rand_int(128) + 1;
   if (doing_trks || doing_rtes) {
@@ -289,17 +296,12 @@ static realtime_data* realtime;
 void
 random_rd_posn_init(const QString&)
 {
-  generator = new std::mt19937;
-  if (opt_seed) {
-    generator->seed(atoi(opt_seed));
-  } else {
-    generator->seed(gpsbabel_now);
-  }
+  random_set_generator();
   realtime = new realtime_data;
   if (opt_points) {
     realtime->points = atoi(opt_points);
   }
-  realtime->time = QDateTime::fromTime_t(gpsbabel_time);
+  realtime->time = current_time();
 }
 
 void
diff --git a/util.cc b/util.cc
index 2bc397a71edd76dd88b2a266a6532d91018d0caf..6eeaff238e8e43b390c777e6372fbc37159bc562 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -715,6 +715,13 @@ mklocaltime(struct tm* t)
   return result;
 }
 
+bool
+gpsbabel_testmode()
+{
+  static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr;
+  return testmode;
+}
+
 /*
  * Historically, when we were C, this was A wrapper for time(2) that
  * allowed us to "freeze" time for testing. The UNIX epoch
@@ -725,7 +732,7 @@ mklocaltime(struct tm* t)
 gpsbabel::DateTime
 current_time()
 {
-  if (getenv("GPSBABEL_FREEZE_TIME")) {
+  if (gpsbabel_testmode()) {
     return QDateTime::fromTime_t(0);
   }
 
diff --git a/xcsv.cc b/xcsv.cc
index 84e605051e9f0dbcebe14eb9141748539571e329..e6627c3ac23da20a9d4f336d8e7cc3bc69aab791 100644 (file)
--- a/xcsv.cc
+++ b/xcsv.cc
@@ -813,7 +813,7 @@ xcsv_parse_val(const char* s, Waypoint* wpt, const field_map& fmp,
     wpt->SetCreationTime(sscanftime(s, fmp.printfc.constData(), 1));
     break;
   case XT_LOCAL_TIME:
-    if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) {
+    if (!gpsbabel_testmode()) {
       wpt->creation_time += sscanftime(s, fmp.printfc.constData(), 0);
     } else {
       /* Force constant time zone for test */
@@ -1763,13 +1763,10 @@ xcsv_replace_tokens(const QString& original) {
     // Don't do potentially expensive replacements if token prefix
     // isn't present;
     if (original.contains("__")) {
-      time_t my_time = gpsbabel_time;
-
       replacement.replace("__FILE__", xcsv_file.fname);
-      replacement.replace("__VERSION__", my_time == 0 ? "" : gpsbabel_version);
+      replacement.replace("__VERSION__", gpsbabel_testmode()? "" : gpsbabel_version);
 
-      QDateTime dt = QDateTime::fromTime_t(my_time);
-      dt = dt.toTimeSpec(Qt::UTC);
+      QDateTime dt = current_time().toUTC();
 
       QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy");
       replacement.replace("__DATE_AND_TIME__", dts);